package com.nd.qa.shareplate.message;

import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;

import com.nd.qa.shareplate.model.ShareJob;
import com.nd.qa.shareplate.service.ShareJobService;
import com.nd.qa.shareplate.util.FormatTimeUtil;
import com.nd.qa.shareplate.util.HttpRequestUtils;

import net.sf.json.JSONObject;

public class InterfaceExecuteTask {
	
	@Autowired
	private ShareJobService shareJobService;
	
	private ResourceBundle rb = ResourceBundle.getBundle("properties/configuration");
	
	public void autoRun(){
		new Thread(){
			public void run() {
				ShareJob shareJob = new ShareJob();
				shareJob.setJobStatus("building");
				shareJob.setJobDesc("true");
				List<ShareJob> list = shareJobService.queryJob(shareJob);
				//循环执行
				for (int i = 0; i < list.size(); i++) {
					final String buildId = list.get(i).getBuildId();
					String environmentType = list.get(i).getEnvironmentType();
					try {
			    		//是否存在未结束的job
			    		final String preUrl = rb.getString("uc.jenkins.lastjob.preurl");
			    		 String jobName = null;
			    		if ("1".equals(environmentType)) {
			    			jobName = rb.getString("uc.jenkins.production.jobname");
			    		}else if ("2".equals(environmentType)) {
			    			jobName = rb.getString("uc.jenkins.preproduction.jobname");
			    		}else if ("3".equals(environmentType)) {
			    			jobName = rb.getString("uc.jenkins.test.jobname");
			    		}else if ("4".equals(environmentType)) {
			    			jobName = rb.getString("uc.jenkins.virtual.jobname");
			    		}else {
			    			jobName = rb.getString("uc.jenkins.aws.jobname");
			    		}
			    		final String lastUrl = rb.getString("uc.jenkins.lastjob.lasturl");
						final String reqUrl = preUrl+jobName+"/"+buildId+lastUrl;
						final JSONObject json = new JSONObject();
						 //Jenkins服务请求
						String strResult = HttpRequestUtils.httpGet(reqUrl);
						//查询结果集
			    		ShareJob sJob = new ShareJob();
			    		sJob.setBuildId(buildId);
			    		sJob.setJobId(list.get(i).getJobId());
			    		sJob.setUpdateTime(FormatTimeUtil.dateToStrLong(new Date()));
						if (strResult!=null&&!"".equals(strResult)) {
							//解析结果集
				    		JSONObject jsonResult = JSONObject.fromObject(strResult);
				    		String building =  String.valueOf(jsonResult.get("building"));
				    		String result =  String.valueOf(jsonResult.get("result"));
				    		if ("SUCCESS".equals(result)||"FAILURE".equals(result)) {
				    			sJob.setJobStatus(result);
				    			sJob.setJobDesc("true");
				    			shareJobService.updateJob(sJob);
							}
				    		if(result==null||result=="null"){
				    			sJob.setJobStatus("building");
				    			sJob.setJobDesc(building);
								shareJobService.updateJob(sJob);
							}
						}else{
							sJob.setJobDesc("false");
							shareJobService.updateJob(sJob);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}.run();
	}
}
